package QianFeng16.JSON.Complex;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class JSONDemo2 {

    /**
     * 使用JsonRead解析复杂的JSON数据
     **/
    @Test
    public void parseJSONMessages() {
        InputStream is = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("src/QianFeng16/JSON/Complex/message.json");
        InputStreamReader in = new InputStreamReader(is);
        JsonReader jsonReader = new JsonReader(in);
        ArrayList<Message> list = readMessageArray(jsonReader);
        for (Message m : list) {
            System.out.println(m.toString());
        }
    }

    //读取Message数组
    private ArrayList<Message> readMessageArray(JsonReader jsonReader) {
        ArrayList<Message> list = new ArrayList<>();
        try {
            //开始解析数组
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                list.add(readMessage(jsonReader));
            }
            //结束解析数组
            jsonReader.endArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    //解析一个Message对象
    private Message readMessage(JsonReader jsonReader) {
        Message message = new Message();
        try {
            //开始解析对象
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                //获取属性名
                String name = jsonReader.nextString();
                //将获取的属性值传递给message对象
                if ("id".equals(name)) {
                    message.setId(jsonReader.nextLong());
                } else if ("text".equals(name)) {
                    message.setText(jsonReader.nextString());
                } else if ("geo".equals(name) && jsonReader.peek() != JsonToken.NULL) {
                    message.setGeo(readGeo(jsonReader));
                } else if ("user".equals(name)) {
                    message.setUser(readUser(jsonReader));
                } else {
                    //表示当所有都不成立的时候,就跳过该字段；解析下一个
                    jsonReader.skipValue();
                }
            }
            //结束对象解析
            jsonReader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;
    }

    //解析geo 返回一个集合
    private ArrayList<Double> readGeo(JsonReader jsonReader) {
        ArrayList<Double> geos = new ArrayList<>();
        try {
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                geos.add(jsonReader.nextDouble());
            }
            jsonReader.endArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return geos;
    }

    //解析user 返回User对象
    private User readUser(JsonReader jsonReader) {
        User user = new User();
        try {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                String name = jsonReader.nextName();//获取User对象的属性名
                if ("name".equals(name)) {
                    user.setName(jsonReader.nextString());
                } else if ("followers_ count".equals(name)) {
                    user.setFollowers_count(jsonReader.nextInt());
                }
            }
            jsonReader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user;
    }



}
